package com.zl.cms.dao;

import com.zl.basic.dao.BaseDao;
import com.zl.cms.model.Channel;
import com.zl.cms.model.ChannelTree;
import com.zl.cms.model.ChannelType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * User: zl email:c3tc3tc3t@126.com Date: 14-2-1 Time: 下午3:19
 */
@Repository("channelDao")
public class ChannelDao extends BaseDao<Channel> implements IChannelDao {

  @Override
  public List<Channel> listByParent(Integer pid) {
    String hql = null;
    if (null == pid || pid == 0) {
      hql = "select c from Channel c where c.parent.id is null order by c.orders asc ";
    } else {
      hql =
          "select c from Channel c left join fetch c.parent cp where cp.id = " + pid
              + " order by c.orders asc ";
    }
    List<Channel> temp = this.list(hql);
    return this.list(hql);
  }

  @Override
  public List<Channel> listPublishChannel() {

    String hql =
        "select new Channel(c.id,c.name) from Channel c where c.status = 1  and c.type != "
            + ChannelType.NAV_CHANNEL.ordinal();
    return this.list(hql);
  }

  @Override
  public Integer getMaxOrderByParent(Integer pid) {
    String hql = null;
    if (null != pid) {
      hql = "select max(c.orders) from Channel c where c.parent.id = " + pid;
    } else {
      hql = "select max(c.orders) from Channel c where c.parent.id is null ";
    }

    Object obj = this.queryObjectByHql(hql);
    if (null == obj) {
      return 0;
    }
    return (Integer) obj;
  }

  @Override
  public List<ChannelTree> generateTree() {
    /*
     * String sql =
     * "select id,name,pid,(select count(*) from t_channel t2 where t2.pid = t1.id) leaf from t_channel t1 order by orders"
     * ;
     */
    String sql = "select id,name,pid from t_channel  order by orders";
    List<ChannelTree> cts = this.listBySql(sql, ChannelTree.class, false);

    initTreeNode(cts);
    return cts;
  }

  public static void initTreeNode(List<ChannelTree> cts) {
    cts.add(0, new ChannelTree(Channel.ROOT_ID, Channel.ROOT_NAME, -1));
    Null2zero(cts);
  }

  private static void Null2zero(List<ChannelTree> cts) {
    for (ChannelTree ct : cts) {
      if (null == ct.getPid()) {
        ct.setPid(0);
      }
      /*
       * if (null == ct.getLeaf()) { ct.setLeaf(0); } else { ct.setLeaf(1); }
       */
    }
  }


  @Override
  public List<ChannelTree> generateTreeByParent(Integer pid) {
    if (null == pid || pid == 0) {
      String sql = "select id,name,pid from t_channel where pid is null order by orders";
      List<ChannelTree> cts = this.listBySql(sql, ChannelTree.class, false);
      Null2zero(cts);
      return cts;
    } else {

      /*
       * String sql = "select  x2.id,x2.name,x2.pid,x1.leaf  " +
       * "from (select count(*) leaf  from t_channel t2 where t2.pid = " + pid + " ) x1  " +
       * "join (select t1.id ,t1.name ,t1.pid  from t_channel t1 where t1.pid = " + pid +
       * " order by orders) x2 " + " on 1=1";
       */
      String sql = "select id,name,pid from t_channel  where pid = " + pid + " order by orders";
      List<ChannelTree> cts = this.listBySql(sql, ChannelTree.class, false);
      Null2zero(cts);
      return cts;
    }
  }

  @Override
  public void updateSort(Integer[] ids) {
    int index = 1;
    String hql = "update Channel c set c.orders = ? where c.id = ?";
    for (Integer id : ids) {
      this.updateByHql(hql, new Object[] {index++, id});
    }

  }
}
